<<template>
  <div class="pic_content">
    <el-row class="info_item">
      <span class="key">
        <span>车</span>
        <span>牌</span>
        <span>号</span>
      </span>
      <span class="value">{{ formData.vehicle_num }}</span>
    </el-row>
    <el-row class="info_item" v-if="Boolean(formData.enter_time)">
      <span class="key">
        <span>进</span>
        <span>场</span>
        <span>时</span>
        <span>间</span>
      </span>
      <span class="value">{{ formData.enter_time }}</span>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-refresh"
        class="refresh_btn"
        @click="checkEnterPic"
        v-if="
          Boolean(formData.enter.device_code) &&
            Boolean(formData.enter.device_num)
        "
        >更新照片
      </el-button>
    </el-row>
    <div
      class="pic_viewer"
      v-loading="enterPicLoading"
      v-if="
        Boolean(formData.enter.device_code) &&
          Boolean(formData.enter.device_num)
      "
    >
      <el-image
        v-for="(item, index) in enterPicMin"
        :key="index"
        class="img"
        fit="cover"
        :src="item"
        :preview-src-list="privewImages(enterPic, index)"
      >
      </el-image>
    </div>
    <div class="no_pic_text" v-if="Boolean(formData.enter_time) && noEnterPic">
      <i class="el-icon-warning"></i>
      未捕获到进场照片！
    </div>
    <el-row class="info_item" v-if="Boolean(formData.leave_time)">
      <span class="key">
        <span>离</span>
        <span>场</span>
        <span>时</span>
        <span>间</span>
      </span>
      <span class="value">{{ formData.leave_time }}</span>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-refresh"
        class="refresh_btn"
        @click="checkLeavePic"
        v-if="
          Boolean(formData.leave.device_code) &&
            Boolean(formData.leave.device_num)
        "
        >更新照片
      </el-button>
    </el-row>
    <div
      class="pic_viewer"
      v-loading="leavePicLoading"
      v-if="
        Boolean(formData.leave.device_code) &&
          Boolean(formData.leave.device_num)
      "
    >
      <el-image
        v-for="(item, index) in leavePicMin"
        :key="index"
        class="img"
        fit="cover"
        :src="item"
        :preview-src-list="privewImages(leavePic, index)"
      >
      </el-image>
    </div>
    <div class="no_pic_text" v-if="Boolean(formData.leave_time) && noLeavePic">
      <i class="el-icon-warning"></i>
      未捕获到离场照片！
    </div>
  </div>
</template>

<script>
import { getList } from "@/api/public";

export default {
  name: "pic_viewer",
  props: {
    formData: {
      default: () => {
        return {
          vehicle_num: 0,
          enter_time: 0,
          leave_time: 0,
          enter: {
            device_code: "",
            device_num: ""
          },
          leave: {
            device_code: "",
            device_num: ""
          }
        };
      }
    }
  },
  data() {
    return {
      // 数据请求loading
      enterPicLoading: false,
      leavePicLoading: false,
      // 进出场照片和缩略图
      enterPicMin: [],
      enterPic: [],
      leavePicMin: [],
      leavePic: [],
      // 若没有照片的提示语显示
      noEnterPic: false,
      noLeavePic: false
    };
  },
  filters: {},
  computed: {},
  watch: {},
  created() {
    this.checkEntrancePic();
  },
  beforeDestroy() {},
  mounted() {},
  methods: {
    // 获取进出场的照片
    checkEntrancePic() {
      if (
        Boolean(this.formData.enter.device_code) &&
        Boolean(this.formData.enter.device_num)
      ) {
        this.checkEnterPic();
      } else {
        this.noEnterPic = true;
      }
      if (
        Boolean(this.formData.leave.device_code) &&
        Boolean(this.formData.leave.device_num)
      ) {
        this.checkLeavePic();
      } else {
        this.noLeavePic = true;
      }
    },
    checkEnterPic() {
      this.enterPicLoading = true;
      if (this.enterPicMin.length > 0) {
        this.enterPicMin = [""];
      } else {
        this.enterPicMin = [];
      }
      getList(
        {
          device_code: this.formData.enter.device_code,
          device_num: this.formData.enter.device_num
        },
        "/mine/iot/capture"
      )
        .then(response => {
          if (response.state) {
            this.enterPicMin =
              response.value.images.map(e => {
                if (e && e.indexOf("?") > -1) {
                  return e + "&height=170";
                } else {
                  return e + "?height=170";
                }
              }) || [];
            this.enterPic = response.value.images || [];
          } else {
            this.enterPicMin = [];
            this.enterPic = [];
            this.$message.error(response.message);
          }
        })
        .catch(error => {
          this.enterPicMin = [];
          this.enterPic = [];
          this.$message.error(response.message);
        })
        .finally(() => {
          setTimeout(() => {
            this.enterPicLoading = false;
            if (this.enterPic.length == 0) {
              this.noEnterPic = true;
            }
          }, 1000);
        });
    },
    checkLeavePic() {
      this.leavePicLoading = true;
      if (this.leavePicMin.length > 0) {
        this.leavePicMin = [""];
      } else {
        this.leavePicMin = [];
      }
      getList(
        {
          device_code: this.formData.leave.device_code,
          device_num: this.formData.leave.device_num
        },
        "/mine/iot/capture"
      )
        .then(response => {
          if (response.state) {
            this.leavePicMin =
              response.value.images.map(e => {
                if (e && e.indexOf("?") > -1) {
                  return e + "&height=170";
                } else {
                  return e + "?height=170";
                }
              }) || [];
            this.leavePic = response.value.images || [];
          } else {
            this.leavePicMin = [];
            this.leavePic = [];
            this.$message.error(response.message);
          }
        })
        .catch(error => {
          this.leavePicMin = [];
          this.leavePic = [];
          this.$message.error(response.message);
        })
        .finally(() => {
          setTimeout(() => {
            this.leavePicLoading = false;
            if (this.leavePic.length == 0) {
              this.noLeavePic = true;
            }
          }, 1000);
        });
    },

    // 调整图片顺序，把当前图片放在第一位
    privewImages(l, index) {
      let list = [...l];
      if (index == 0) return list;
      let start = list.splice(index);
      let remain = list.splice(0, index);
      return start.concat(remain);
    }
  }
};
</script>

<style lang="scss" scoped>
.pic_content {
  border-top: 1px solid #eee;
  padding: 20px;
  .info_item {
    line-height: 35px;
    color: #333;
    font-size: 24px;
    margin-bottom: 10px;
    .key {
      display: inline-flex;
      justify-content: space-between;
      width: 100px;
      color: #999999;
      margin-right: 20px;
      text-align: center;
    }
  }
  .pic_text {
    color: #fa5858;
    font-size: 20px;
    line-height: 30px;
    margin-bottom: 5px;
  }
  .refresh_btn {
    position: absolute;
    right: 0px;
    top: 0px;
    border-color: #979797;
    background: #fff;
    color: #666;
    &:hover {
      background: #f7f7f7;
    }
  }
  .pic_viewer {
    min-height: 170px;
    max-height: 400px;
    overflow: auto;
    margin: 0 -5px;
    position: relative;

    .img {
      width: calc(50% - 10px);
      height: 170px;
      max-width: 285px;
      margin: 5px;
      cursor: pointer;
    }
  }
  .no_pic_text {
    color: #ff5151;
    line-height: 50px;
    font-size: 24px;
    margin-bottom: 10px;
  }
}
.pic_toolbar {
  border-top: 1px solid #eee;
  padding: 20px;
  display: inline-flex;
  justify-content: end;
  width: 100%;
  .info_text {
    color: #999;
    font-size: 20px;
    line-height: 48px;
    margin-right: 10px;
  }
  button {
    border: 1px solid #d9d9d9;
    background: #eee;
    color: #3344dd;
    font-size: 24px;
    text-indent: 1em;
    letter-spacing: 1em;
    text-align: center;
  }
}
</style>
